<?php
Modulator()->registerPanel('Newsletter', 'newsletterPanel');
Modulator()->registerFunction('admin_sidebar', 'newsletterSidebar');

function newsletterPanel() {
	?>
	This is the newsletter panel
	<?php
}



function newsletterSignup() {
	global $concerto;
	$display = TRUE;
	$alerts = array();
	if($_REQUEST['newsletter_subscribe']) {
		/** validate e-mail */
		$email = $_REQUEST['email'];
		if(!\application\datatypes\Email::validateEmail($email)) {
			$alerts[] = 'Please enter a valid e-mail address.';
		}
		if(DB()->getCount("SELECT `sub_id` FROM `newsletter_subscriptions` WHERE `sub_email`='$email'")) {
			$alerts[] = 'You have already subscribed to our newsletter.';
		}
		
		if(!count($alerts)) {
			$insert = array('sub_email'=>$email);
			DB()->insert('newsletter_subscriptions', $insert);
			?>
			<div id="newsletter_subscribe"><p>Thanks for signing up!</p></div>
			<?php
			$display = FALSE;
		}
	}
	$email = $concerto->User->dbFields['user_email'];
	if($display) {
		if(loggedIn()) {
			if(!DB()->getCount("SELECT `sub_id` FROM `newsletter_subscriptions` WHERE `sub_email`='$email'")) {
				?>
				<form id="newsletter-subscribe" method="POST">
					<div id="newsletter-alert">
						<?php if(count($alerts)) : ?>
							<p><?=implode('</p><p>', $alerts)?></p>
						<?php endif;?>
					</div>
					<p>
					<label>E-Mail</label>
					<input type="text" value="<?=$email?>" name="email"/>
					</p>
					<input type="submit" name="newsletter_subscribe" value="Signup"/>
					
				</form>
				
				<?php
			}
			else {
				?>
				<div id="newsletter-subscribe">
					<p>Thanks for signing up for our newsletter!</p>
				</div>
				<?php
			}
		}
		else {
			?>
			<form id="newsletter-subscribe" method="POST">
				<div id="newsletter-alert">
					<?php if(count($alerts)) : ?>
						<p><?=implode('</p><p>', $alerts)?></p>
					<?php endif;?>
				</div>
				<p>
				<label>E-Mail</label>
				<input type="text" value="" name="email"/>
				</p>
				<input type="submit" name="newsletter_subscribe" value="Signup"/>
					
			</form>
			<?php
		}
	}

}

?>